<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Isee 8583 主页</title>
    <base href="<?php echo base_url();?>">

    <!-- Bootstrap core CSS -->
    <link href="./style/css/bootstrap.min.css" rel="stylesheet">
    <!--<link href="bootstrap.min.css" rel="stylesheet">-->

    <!-- Custom styles for this template -->

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <![endif]-->
    <!--[if lt IE 9]>
    <script src="./style/js/respond.min.js"></script>
    <script src="./style/js/html5shiv.min.js"></script>
    <![endif]-->
    <style type="text/css">
        html {
            -ms-overflow-style:none;
            overflow:-moz-scrollbars-none;
        }
        html::-webkit-scrollbar{width:0px}
        body{
            padding-top: 50px;
        }
        .sidebar{
            display: none;
        }
        @media (min-width: 768px){
            .sidebar{
                position: fixed;
                top: 51px;
                bottom: 0px;
                left: 0px;
                z-index: 1000;
                display: block;
                padding: 20px;
                overflow-x: hidden;
                overflow-y: auto;
                background-color: #f5f5f5;
                border-right: 1px solid #eee;
                /*border: 1px solid red;*/
            }
        }
        .nav-sidebar{
            margin-right: -21px;
            margin-bottom: 20px;
            margin-left: -20px;
        }
        .nav-sidebar > li > a{
            padding-right: 20px;
            padding-left: 20px;
        }
        .nav-sidebar > .active > a,
        .nav-sidebar > .active > a:hover,
        .nav-sidebar > .active > a:focus{
            color: #fff;
            background-color: #428bca;
        }
        .main{
            width: 100%;
            margin: 40px auto;
        }
        .main-title, th, td{
            text-align: center;
        }
        .main-title{
            height: 10%;
            /*border: 1px solid red;*/
            margin-bottom: 0;
            margin-top: 0;
            padding-top: 5px;
        }
        .table-responsive{
            height: 70%;
            /*border: 1px solid red;*/

        }
        .pagination-father-box{
            height: 20%;
            /*border: 1px solid red;*/
        }

        .jumbotron{
            padding-bottom: 0;
            border: 1px solid green;
            position: relative;
            height: 500px;
            width: 100%;
            padding-top: 0;
        }
        .jumbotron h1 {
            font-size: 40px;
        }
        .form-control{
            overflow: hidden;
            overflow-y: scroll;
        }
        .buttons{
            position: relative;
            left: -15px;
        }

        .my-account li{
            padding-top: 0px;
            padding-bottom: 0px;
            text-align: center;
        }
        .my-account li a{
            font-size: 20px;
        }
    </style>
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">下拉菜单</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="./home">主页</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的 <span class="caret"></span></a>
                    <ul class="my-account dropdown-menu">
                        <li><?php echo $email;?></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="./account/password.html">修改密码</a></li>
                        <li><a href="./login/logout.html">注销</a></li>
                    </ul>
                </li>
                <li><a href="./help.html">帮助</a></li>
            </ul>
        </div>
    </div>
</nav>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="./home.html">开始监控</a></li>
                <li><a href="./template/create.html">新建8583模板</a></li>
                <li><a href="./address/create.html">添加后台地址</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2">
            <div class="row main"">
                <div class="main-template   col-sm-6 col-md-6">
                    <div class="jumbotron " >
                        <h1 class="main-title">8583模板</h1>
                        <!--table-->
                        <div class="table-responsive">
                            <table class="table table-striped" >
                                <thead >
                                <tr >
                                    <th width="8%">状态</th>
                                    <th width="31%">客户</th>
                                    <th width="31%">项目</th>
                                    <th width="15%" colspan="2">操作</th>
                                </tr>
                                </thead>
                                <tbody class="template-list">
                                <?php foreach($templates as $row): ?>
                                <tr class="select_line" id="<?php echo $row['id']; ?>">
                                    <td align="center"><input type="radio"  name="t8583" class="t8583" value="<?php echo $row['id']; ?>"></td>
                                    <td class="tr-customer"><?php echo $row['customer']; ?></td>
                                    <td class="tr-name"><?php echo $row['name']; ?></td>
                                    <td><button type="button" class="btn btn-info editer_t8583" >修改</button></td>
                                    <td><button type="button" class="btn btn-danger del_t8583">删除</button></td>
                                </tr>
                                <?php endforeach ?>
                                </tbody>

                            </table>

                        </div>
                        <!--pagination-->
                        <div class="text-center pagination-father-box" >
                            <nav class="pagination-box template-pagination">
                                <?php echo $links;?>
                            </nav>
                        </div>
                    </div>
                </div>

                <div class="main-address   col-sm-6 col-md-6 ">
                    <div class="jumbotron ">
                        <h1 class="main-title">后台地址</h1>
                        <div class="table-responsive">
                            <table class="table table-striped" >
                                <thead >
                                <tr >
                                    <th width="8%">状态</th>
                                    <th width="31%">名称</th>
                                    <th width="31%">地址</th>
                                    <th width="15%" colspan="2">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach($address as $row):?>
                                <tr class="select_line" id="<?php echo $row['id'];?>">
                                    <td align="center"><input type="radio"  name="server_addr" class="selected" value="<?php echo $row['id'];?>" ></td>
                                    <td class="name"><?php echo $row['name'];?></td>
                                    <td class="ip-port"><?php echo $row['ip'] . ":" . $row['port']; ?></td>
                                    <td class="edit"><button type="button" class="btn btn-info" >修改</button></td>
                                    <td class="del"><button type="button" class="btn btn-danger">删除</button></td>
                                </tr>
                                <?php endforeach?>
                                </tbody>

                            </table>

                        </div>
                        <!--pagination-->
                        <div class="text-center pagination-father-box">
                            <nav class="pagination-box">
                                <?php echo $addr_links;?>
                            </nav>
                        </div>
                    </div>

                </div>
            <!--<div class="row buttons">-->
                <!--<div class="button-group">-->
                    <!--&lt;!&ndash;<button type="button" class="btn btn-lg btn-success button-begin">启动监控</button>&ndash;&gt;-->
                    <!--&lt;!&ndash;<button type="button" class="btn btn-lg btn-success button-begin">原始报文监控</button>&ndash;&gt;-->
                <!--</div>-->
            <!--</div>-->
            <div class="row buttons-box">
                <div class="start-buttons" style="text-align: center">
                    <a  class="start-iso-btn btn btn-lg btn-success" style="display: inline" target="_blank">8583监控</a>
                    <a href="#" class="start-raw-btn btn btn-lg btn-success" style="display: inline" target="_blank">原始监控</a>
                </div>
            </div>
        </div>

    </div>

</div>


<div class="modal fade" id="edit-t8583" tabindex="-1" role="dialog" aria-labelledby="edit-t8583-title">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="edit-t8583-title">编辑模板</h4>
            </div>
            <div class="modal-body">
                <div class="row" style="height: 500px">
                    <form id="template-form">
                        <span class="template-id" hidden></span>
                        <div class="col-lg-4 col-lg-offset-4 template-name-box">
                            <input class="form-control customer" type="text" placeholder="客户" required autofocus name="customer">
                            <input class="form-control template-name" type="text" placeholder="项目" required name="name">
                        </div>
                        <div class="col-md-12 template-table" style="overflow-x: hidden">
                            <table class="table table-bordered table-hover table-striped table-header" >
                                <thead>
                                <tr>
                                    <th class="big-title">域</th>
                                    <th class="big-title" colspan="2">长度字段</th>
                                    <th class="big-title" colspan="2">数据字段</th>
                                </tr>
                                <tr>
                                    <th class="th1">#</th>
                                    <th class="th2">类型</th>
                                    <th class="th3">编码</th>
                                    <th class="th4">最大长度</th>
                                    <th class="th5">编码</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                            <div class="col-lg-12  template-table" style="height: 500px; overflow-y:auto; margin-right: -15px; ">
                                <table class="table table-bordered table-hover table-striped" >
                                    <tbody style="height: 100px">
                                    <?php for ($i = 1; $i <= 64; $i++):?>
                                    <tr id="<?php echo 'tr'.$i;?>">
                                        <td class="td1">
                                            <select class="form-control field_num" name="field_num[]">
                                                <option class="num"  value="<?php echo $i;?>"><?php echo $i;?></option>
                                                <option class="not-use" value="nil">不存在</option>
                                            </select>
                                        </td>
                                        <td class="td2">
                                            <select class="form-control len_section_select" name="len_section_type[]">
                                                <option class="llvar" value="llvar">LLVAR</option>
                                                <option class="lllvar" value="lllvar">LLLVAR</option>
                                                <option class="fix" value="fix">FIX</option>
                                            </select>
                                        </td>
                                        <td class="td3">
                                            <select class="form-control len_section_encode_select" name="len_section_encode[]">
                                                <option class="bcd_left" value="bcd_left">BCD-LEFT</option>
                                                <option class="bcd_right" value="bcd_right">BCD-RIGHT</option>
                                                <option class="ascii" value="ascii">ASCII</option>
                                                <option class="hex" value="hex">HEX</option>
                                            </select>
                                        </td>
                                        <td class="td4">
                                            <div class="form-group">
                                                <input type="text" class="form-control data_section_len" name="data_section_len[]" id="data_section_len" value="1">
                                            </div>
                                        </td>
                                        <td class="td5">
                                            <select class="form-control data_section_encode" name="data_section_encode[]">
                                                <option class="bcd_left" value="bcd_left">BCD-LEFT</option>
                                                <option class="bcd_right" value="bcd_right">BCD-RIGHT</option>
                                                <option class="ascii" value="ascii">ASCII</option>
                                                <option class="bin" value="bin">BIN</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <?php endfor; ?>
                                    </tbody>
                                </table>
                            </div>
                    </form>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary submit-edit-template">提交修改</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="edit-server-addr" tabindex="-1" role="dialog" aria-labelledby="edit-server-addr-title">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="edit-server-addr-title">修改后台地址</h4>
            </div>
            <div class="modal-body">
                <form class="">
                    <span class="sr-only id"></span>
                    <div class="form-group" style="">
                        <div class="input-group" style="width: 100%">
                            <input type="text" class="form-control name" id="project-name" name="name" required autofocus>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="server-ip">IP</label>
                        <div class="input-group">
                            <div class="input-group-addon">IP</div>
                            <input type="text" class="form-control ip" id="server-ip" name="ip" required autofocus>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="server-port">PORT</label>
                        <div class="input-group">
                            <div class="input-group-addon">PORT</div>
                            <input type="text" class="form-control port" id="server-port" name="port" required>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default cancel" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary ok">提交修改</button>
            </div>
        </div>
    </div>
</div>


<!-- Bootstrap core JavaScript
   ================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./style/js/jquery-1.12.3.js"></script>
<script src="./style/js/bootstrap.min.js"></script>
<script src="./style/js/modal.js"></script>
<!-- Just to make our placeholder images work. Don't actually copy the next line! -->
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script type="text/javascript">
    window.onload = function () {

        function change_active_button(obj) {
                obj.parent().parent().parent().children('tr').removeClass('danger');
                obj.parent().parent().parent().children('tr').removeClass('success');
                obj.parent().parent().removeClass('active').addClass('danger');
                obj.parent().parent().parent().find('.t8583').removeAttr('checked');
                obj.parent().parent().parent().find('.server_addr').removeAttr('checked');
        }

        function change_active_checkbox(obj) {
            obj.parent().parent().parent().children('tr').removeClass('success');
            obj.parent().parent().parent().children('tr').removeClass('danger');
            obj.parent().parent().addClass('success');
        }
        
        function create_tr_template(e) {

           return $('<tr></tr>',{
                class:'select_line',
                id:e.id
            }).append(
                    $('<td></td>',{
                        align:'center'
                    }).append(
                            $('<input>',{
                                type:'radio',
                                name:'t8583',
                                class:'t8583',
                                value:'1'
                            })
                    )
            ).append(
                    $('<td></td>',{
                        class:'tr-customer',
                        text:e.customer
                    })
            ).append(
                    $('<td></td>',{
                        class:'tr-name',
                        text:e.name
                    })
            ).append(
                    $('<td></td>',{
                    }).append(
                            $('<button></button>', {
                                type:'button',
                                class:'btn btn-info editer_t8583',
                                text:'修改'
                            })
                    )
            ).append(
                    $('<td></td>',{
                    }).append(
                            $('<button></button>', {
                                type:'button',
                                class:'btn btn-danger del_t8583',
                                text:'删除'
                            })
                    )
            );
        }


        function create_tr_address(e) {
            return $('<tr></tr>',{
                class:'select_line',
                id:e.id
            }).append(
                    $('<td></td>',{
                        align:'center'
                    }).append(
                            $('<input>',{
                                type:'radio',
                                name:'server_addr',
                                class:'selected',
                                value:'1'
                            })
                    )
            ).append(
                    $('<td></td>',{
                        class:'name',
                        text:e.name
                    })
            ).append(
                    $('<td></td>',{
                        class:'ip-port',
                        text:e.ip + ":" + e.port
                    })
            ).append(
                    $('<td></td>',{
                    }).append(
                            $('<button></button>', {
                                type:'button',
                                class:'btn btn-info edit',
                                text:'修改'
                            })
                    )
            ).append(
                    $('<td></td>',{
                        class:'del'
                    }).append(
                            $('<button></button>', {
                                type:'button',
                                class:'btn btn-danger',
                                text:'删除'
                            })
                    )
            );
        }

        $('.main-address').on(
                'click',
                '.del button',
                function () {
                    change_active_button($(this));

                    var modal = Ewin.confirm({title:'删除该地址', message:'是否删除?'});
                    var del_me = $(this);

                    modal.on(function (status) {
                        if (!status){
                            return;
                        }
                        var item = del_me.parent().parent();
                        var is_last_last = false;

                        if (item.next().attr('id') == undefined && item.prev().attr('id') == undefined){
                            is_last_last = true;
                        }

                        var current_page_url = $('.main-address .pagination-box .active').next().find('a').attr('href');
                        if (current_page_url != undefined){
                            var page_segments =  current_page_url.split('/');
                            var page = Number(page_segments.pop()) - 5;
                            page_segments.push(page);
                            current_page_url = page_segments.join('/');
                        }else {
                            current_page_url = $('.main-address .pagination-box .active').prev().find('a').attr('href');
                            if (current_page_url != undefined){
                                var page_segments =  current_page_url.split('/');
                                var page = Number(page_segments.pop()) + 5;
                                if (is_last_last){
                                    page = page - 5;
                                }

                                page_segments.push(page);
                                current_page_url = page_segments.join('/');
                            }else{
                                current_page_url = './home/address/0'
                            }
                        }
                        $.ajax({
                            type:'post',
                            url:'./address/delete.html',
                            async:true,
                            data:{
                                id:item.attr('id')
                            },
                            dataType:'json',
                            success:function (json) {
                                if (json.code != 'ok'){
                                    window.location = './home.html';
                                }
                                load_address_one_page(current_page_url, $('.main-address .pagination-box').find('ul'));
                            },
                            error:function (xhr, textStatus, msg) {
                                Ewin.alert({title:'删除地址', message:msg});
                                window.location = './home.html';
                            }

                        });
                    });

                }
        );



        $('.template-list').on(
                'click',
                '.del_t8583',
                function () {
                    change_active_button($(this));
                    var modal = Ewin.confirm({title:'删除模板', message:'是否删除?'});

                    var del_me = $(this);

                    modal.on(function (status) {
                        if (!status) {
                            return;
                        }
                        var obj = del_me.parent().parent();
                        var is_last_last = false;

                        if (obj.next().attr('id') == undefined && obj.prev().attr('id') == undefined){
                            is_last_last = true;
                        }


                        var current_page_url = $('.template-pagination .active').next().find('a').attr('href');
                        if (current_page_url != undefined){
                            var page_segments =  current_page_url.split('/');
                            var page = Number(page_segments.pop()) - 5;
                            page_segments.push(page);
                            current_page_url = page_segments.join('/');
                        }else {
                            current_page_url = $('.template-pagination .active').prev().find('a').attr('href');
                            if (current_page_url != undefined){
                                var page_segments =  current_page_url.split('/');
                                var page = Number(page_segments.pop()) + 5;
                                if (is_last_last){
                                    page = page - 5;
                                }

                                page_segments.push(page);
                                current_page_url = page_segments.join('/');
                            }else{
                                current_page_url = './home/template/0'
                            }
                        }
                        $.ajax({
                            type:'post',
                            url:'./template/delete.html',
                            async:true,
                            data:{
                                id:obj.attr('id')
                            },
                            dataType:'json',
                            success:function (json) {
                                if (json.code != 'ok'){
                                    window.location = './home.html';
                                }
                                load_template_one_page(current_page_url, $('.template-pagination').find('ul'));
                            },
                            error:function () {
                                window.location = './home.html';
                            }

                        });




                    })

                }
        );

        $('.main-address tbody').on(
                'click',
                '.edit button',
                function () {
                    change_active_button($(this));
                    var id = $(this).parent().parent();

                    var name = id.find('.name').text();
                    var ip_port = id.find('.ip-port').text();
                    var ip = ip_port.split(':').shift();
                    var port = ip_port.split(':').pop();



                    $('#edit-server-addr .name').val(name);
                    $('#edit-server-addr .ip').val(ip);
                    $('#edit-server-addr .port').val(port);
                    $('#edit-server-addr .id').text(id.attr('id'));

                    $('#edit-server-addr').modal({
                        backdrop: 'static'
                    });

                }
        );


        $('.template-list').on('click', '.editer_t8583', function (event) {
            change_active_button($(this));

            var id = $(this).parent().parent().attr('id');

            $.ajax({
                type:'post',
                url:'./template/get.html',
                async:true,
                data:{
                  id:id
                },
                dataType:'json',
                success:function (json) {
                    if (json.code != 'ok'){
                        alert(json.code);
                        return;
                    }
                    $('#template-form .customer').val(json.template_info.customer);
                    $('#template-form .template-name').val(json.template_info.name);
                    $('#template-form .template-id').text(id);

                    for(var i = 1; i <= 64; i++){
                        var e = json.template_info.content.shift();

                        var trid = "#tr" + i;

                        var tr = $(trid);


                        if (e.field == 'nil') {
                            $(tr).find(".td1").find("select").find('option[value="nil"]').attr('selected', true);
                        }

                        $(tr).find(".td2").find("select").find("." + e.type).attr("selected", true);
                        $(tr).find(".td3").find("select").find("." + e.len_encode).attr("selected", true);
                        $(tr).find(".td4").find("input").val(e.data_len);
                        $(tr).find(".td5").find("select").find("." + e.data_encode).attr("selected", true);
                        if (e.type == 'fix'){
                            $(tr).find(".td3").find("select").attr("disabled", "disabled");
                        }
                    }

                    $('.template-table tbody tr').each(function () {
                        var select = $(this).find('.td1 select').find('option[selected="selected"]');

                        if ($(select).attr('value') != 'nil'){
                            $(this).css('background-color', ' #8cd9b3');
                        }else{
                            $(this).css('background-color', ' #ccc');
                        }
                        select = $(this).find('.td2 select').find('option[selected="selected"]');
                        if ($(select).attr('value') == 'fix'){
                            $(this).find('.td3 select').attr('disabled', 'disabled');
                        }else{
                            $(this).find('.td3 select').removeAttr('disabled');
                        }


                        $(this).find('.td1 select').change(function () {
                            if($(this).val() != 'nil'){
                                $(this).parent().parent().css('background-color', '#8cd9b3');
                            }else{
                                $(this).parent().parent().css('background-color', '#ccc');
                            }
                        });
                    });
                }

            });

            $('#edit-t8583').modal({
                backdrop: 'static'
            });

        });

        $('#edit-server-addr button.ok').click(function () {
            var id = $('#edit-server-addr .id').text();
            var name = $('#edit-server-addr .name').val();
            var ip = $('#edit-server-addr .ip').val();
            var port = $('#edit-server-addr .port').val();


            $.ajax({
                type:'post',
                url:'./address/update.html',
                async:true,
                data:{
                    id:id,
                    name:name,
                    ip:ip,
                    port:port
                },
                dataType:'json',
                success:function (json) {
                    if (json.code != 'ok'){
                        Ewin.alert({title: '编辑后台地址', message:json.msg});
                        return;
                    }
                    $('#edit-server-addr').modal('hide');
                    var modal = Ewin.alert({title: '编辑后台地址', message: '修改成功'});
                    var item = $('.main-address #' + id);
                    item.find('.name').text(name);
                    item.find('.ip-port').text(ip + ":" + port);
                    return;
                },
                error:function (xhr, textStatus, msg) {
                    Ewin.alert({title: '编辑后台地址', message:msg});
                    return;
                }
            });


        });






        $('.submit-edit-template').click(function () {
            var field_num = [];
            var len_section_type = [];
            var len_section_encode = [];
            var data_section_len = [];
            var data_section_encode = [];

            $('#template-form tbody .field_num').each(function () {
                field_num.push($(this).val());
            });

            $('#template-form tbody .len_section_select').each(function () {
               len_section_type.push($(this).val());
            });

            $('#template-form tbody .len_section_encode_select').each(function () {
                len_section_encode.push($(this).val());
            });
            $('#template-form tbody .data_section_len').each(function () {
                data_section_len.push($(this).val());
            });

            $('#template-form tbody .data_section_encode').each(function () {
                data_section_encode.push($(this).val());
            });
            $.ajax({
                type:'post',
                url:'./template/update.html',
                async:true,
                data:{
                    id:$('#template-form .template-id').text(),
                    customer: $('#template-form .customer').val(),
                    name:$('#template-form .template-name').val(),
                    field_num:field_num,
                    len_section_type:len_section_type,
                    len_section_encode:len_section_encode,
                    data_section_len:data_section_len,
                    data_section_encode:data_section_encode
                },
                dataType:'json',
                success:function (json) {
                    if (json.code == 'ok'){
                        $('#edit-t8583').modal('hide');
                            var modal = Ewin.alert({title: '编辑模板', message: '修改成功'});
                            modal.on();
                            var customer = $('#template-form .customer').val();
                            var name = $('#template-form .template-name').val();
                            var id= $('#template-form .template-id').text();

                            $('#' + id + ' .tr-customer').text(customer);
                            $('#' + id + ' .tr-name').text(name);
                            return;
                    }else{
                        var modal = Ewin.alert({title: '编辑模板', message: json.msg});
                        modal.on();
                        return;
                    }

                },
                error:function (xhr, textStatus, msg) {
                    alert(msg);
                    return;
                }

            })

        });












        $('.len_section_select').change(function () {
            var len_section_encode_select = $(this).parent().parent().find('.len_section_encode_select');
            var type = $(this).val();
            if (type == 'fix'){
                len_section_encode_select.attr('disabled', 'disabled');
            }else{
                len_section_encode_select.removeAttr('disabled');
            }

        });

        function load_address_one_page(url, pagination) {
            var all_addr = new Array();
            var links = '';
            $.ajax({
                type:'get',
                url:url,
                async:true,
                dataType:'json',
                success:function (data) {
                    if (data.code == 'ok'){
                        links = data.addr_links;
                        pagination.html(links);
                        all_addr = data.address;
                        $('.main-address tbody').empty();
                        while (all_addr.length){
                            var e = all_addr.shift();
                            var tr = create_tr_address(e);
                            $('.main-address tbody').append(tr);
                        }
                    }
                },
                error:function (jqxhr, textStatus, errMsg) {
                    Ewin.alert({title:'加载地址列表', message:msg});
                    window.location = './home.html';
                }
            });
        }


        function load_template_one_page(url, pagination) {
            var templates = new Array();
            var links = '';
            $.ajax({
                type:'get',
                url:url,
                async:true,
                dataType:'json',
                success:function (data) {
                    if (data.code == 'ok'){
                        links = data.links;
                        pagination.html(links);
                        templates = data.templates;
                        $('.template-list').empty();
                        while (templates.length){
                            var e = templates.shift();
                            var tr = create_tr_template(e);
                            $('.template-list').append(tr);
                        }
                    }
                },
                error:function (jqxhr, textStatus, errMsg) {
                    alert(errMsg);
                    window.location = './home.html';
                }
            });
        }

        $('.main-address .pagination-box').on(
                'click',
                '.get_page_address',
                function () {
                    var a = $(this).find('a');
                    var pagination = $(this).parent().parent();

                    load_address_one_page(a.attr('href'), pagination);
                    return false;

                }
        );

        $('.pagination-box').on(
                'click',
                '.get_page_template',
                function () {
                    var a = $(this).find('a');
                    var pagination = $(this).parent().parent();

                    load_template_one_page(a.attr('href'), pagination);
                    return false;

                }
        );
        
        $('.start-iso-btn').click(function () {
            var tid = $('.main-template tbody input[name="t8583"]:checked').val();
            if (tid == undefined){
                alert("请先选择一个模板");
                return false;
            }
            var sid = $('.main-address tbody input[name="server_addr"]:checked').val();
            if (sid == undefined){
                alert("请先选择一个后台地址");
                return false;
            }
            $(this).attr('href', './node/create_iso?' + 'sid=' + sid + '&tid=' + tid);
            return true;
        });


        $('.start-raw-btn').click(function () {
            var sid = $('.main-address tbody input[name="server_addr"]:checked').val()
            if (sid == undefined){
                alert("请先选择一个后台地址");
                return false;
            }
            $(this).attr('href', './node/create_raw?' + 'sid=' + sid);
            return true;
        });


        $('.template-list, .main-address tbody').on('change', '.t8583, .selected', function () {
            change_active_checkbox($(this));
        });
    }
</script>
</body>
</html>

